﻿using BLL;
using System;
using System.Data;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Print
{
    public partial class WeekFundPrint : System.Web.UI.Page
    {
        //  private Qs_TheirBLL tbll = new Qs_TheirBLL();

        private decimal _projectid;

        private Qs_Their_MoneyBLL thbll = new Qs_Their_MoneyBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //获取项目的编号

                if (!String.IsNullOrEmpty(Request.QueryString["PID"].ToString()))
                {
                    _projectid = decimal.Parse(Request.QueryString["PID"].ToString());
                }
                else
                {
                    return;
                }
                if (HttpContext.Current.Session["SysUserInfo"] != null)
                {
                    if (_projectid.ToString() != null && _projectid.ToString() != "")
                    {
                        DataTable dt = new BLL.Qs_Week_SonBLL().Print(_projectid.ToString());

                        DataTable dtpro = null;
                        if (dt != null && dt.Rows.Count > 0)
                        {
                            dtpro = new Lc_TransactionBLL().GetTran(_projectid);
                        }

                        DrawData(dt, dtpro);
                    }
                }
                else
                {
                    //MessageBox.ResponseScript(Page, "alert('【系统提示】对不起，您的登录已过期，请重新登录！');window.close();");
                }
            }
        }

        #region 方法

        public void DrawData(DataTable dt, DataTable dtTran)
        {
            int allCount = dt.Rows.Count;
            int pageCount = 13;

            if (dt.Rows.Count < 1)
            {
                MessageBox.Show(Page, "无工程用量！！！");
                return;
            }

            HtmlGenericControl ht = null;
            HtmlGenericControl myThead = null;
            HtmlGenericControl myTbody = null;
            HtmlGenericControl myTfoot = null;
            HtmlGenericControl myTable = null;

            HtmlTableRow row = null;

            #region 创建主表

            Table tb = new Table();
            tb.Width = Unit.Percentage(100);
            TableRow tr = new TableRow();
            TableCell td = new TableCell();
            td.VerticalAlign = VerticalAlign.Top;
            td.BorderStyle = BorderStyle.None;
            td.HorizontalAlign = HorizontalAlign.Center;
            myTable = CreateHTMLMainTable("95%", "border-collapse:collapse;", string.Empty);
            myThead = new HtmlGenericControl("thead");
            myThead.Attributes.Add("style", "display:table-header-group;font-weight:bold");
            myTbody = new HtmlGenericControl("tbody");
            myTfoot = new HtmlGenericControl("tfoot");
            myTfoot.Attributes.Add("style", "display:table-footer-group;font-weight:bold");
            myTable.Controls.Add(myThead);
            myTable.Controls.Add(myTbody);
            myTable.Controls.Add(myTfoot);
            td.Controls.Add(myTable);
            tr.Controls.Add(td);
            tb.Controls.Add(tr);
            form1.Controls.Add(tb);
            #endregion 创建主表

            #region 创建数据表格
            ht = CreateHTMLMainTable("100%", "border-top-width:0px", "td_table");//创建一个Table作为表体string.Empty

            int length = allCount / pageCount;

            if (allCount % pageCount != 0)
            {
                length++;
            }
            int i = 1;
            for (i = 1; i <= length; i++)
            {
                if (i == 1)
                {
                    ht = FillDataToHtmlHeadTable(dt);
                }
                //DataTable dtList = GetListDataOfOnePage(dt, pageCount, i);
                DataTable dtList = dt;
                ht = CreateHTMLMainTable("100%", "border-top-width:0px", "td_table");

                ht = FillDataToHtmlHeadTable(dt);

                ht = FillDataToHtmlTable(dtList, ht, "td_tableData");//插入数据行

                int empCount = pageCount - dt.Rows.Count;
                if (empCount > 0)
                {
                    ht = FillEmptyDataToHtmlTable(empCount, ht, "td_tableData");//插入空行行
                }

                if (i == length)
                {
                    #region

                    row = CreateHTMLRow("15px", null, null);
                    ht.Controls.Add(row);

                    row = CreateHTMLRow(null, null, null);
                    row.Cells.Add(CreateHTMLCell("办理过程", "width:15%;border:1px solid;border-color:black;font-size:11pt;font-weight:bold;", "center", "td_tableHead11", 1));
                    row.Cells.Add(CreateHTMLCell("办理人", " width:12%;border:1px solid;border-color:black;font-size:11pt;font-weight:bold; ", "center", "td_tableHead11", 1));
                    row.Cells.Add(CreateHTMLCell("办理状态", " width:12%;border:1px solid;border-color:black;font-size:11pt;font-weight:bold; ", "center", "td_tableHead11", 1));
                    row.Cells.Add(CreateHTMLCell("办理日期", " width:16%;border:1px solid;border-color:black;font-size:11pt;font-weight:bold; ", "center", "td_tableHead11", 1));
                    row.Cells.Add(CreateHTMLCell("办理意见", " width:15%;border:1px solid;border-color:black;font-size:11pt;font-weight:bold; ", "center", "td_tableHead11", 1));
                    row.Cells.Add(CreateHTMLCell("签章", " width:15%;border:1px solid;border-color:black;font-size:11pt;font-weight:bold; ", "center", "td_tableHead11", 1));
                    ht.Controls.Add(row);

                    DataRow dr = null;
                    if (dtTran.Rows.Count > 0)
                    {
                        int rowCount = dtTran.Rows.Count;

                        for (int j = 0; j < rowCount; j++)
                        {
                            dr = dtTran.Rows[j];
                            row = CreateHTMLRow(null, null, null);
                            row.Cells.Add(CreateHTMLCell(dr["NodeName"].ToString() == "" ? "&nbsp" : dr["NodeName"].ToString(), "  border-top-width:0px;border-left-width:2px;border:1px solid; border-color:black;", "center", "td_tableHead11", 1));
                            row.Cells.Add(CreateHTMLCell(dr["c_transactor_id"].ToString() == "" ? "&nbsp;" : GetUserName(dr["c_transactor_id"].ToString()), " border-top-width:0px; border:1px solid;border-color:black;", "center", "td_tableHead11", 1));
                            row.Cells.Add(CreateHTMLCell(dr["state"].ToString() == "" ? "&nbsp;" : dr["state"].ToString(), " border-top-width:0px; border:1px solid; border-color:black;", "center", "td_tableHead11", 1));
                            row.Cells.Add(CreateHTMLCell(dr["startTime"].ToString() == "" ? "&nbsp;" : dr["startTime"].ToString(), "border-top-width:0px; border:1px solid; border-color:black;", "center", "td_tableHead11", 1));
                            row.Cells.Add(CreateHTMLCell(dr["coment"].ToString() == "" ? "&nbsp;" : dr["coment"].ToString(), "  border-top-width:0px; border:1px solid;border-color:black; ", "center", "td_tableHead11", 1));

                            HtmlTableCell cell = new HtmlTableCell();

                            cell = CreateHTMLCell("&nbsp;", " border-top-width:0px; border:1px solid;border-color:black; ", "center", "td_tableHead11", 1);
                            Image imgSignatrue = new Image();
                            string url = "";
                            if (dr["signatureid"].ToString() != "")
                            {
                                string signid = dr["signatureid"].ToString();
                                DataTable dtU = new Sys_SignatureBLL().GetSignatureDataByID(Convert.ToDecimal(signid.ToString()));
                                if (dtU != null && dtU.Rows.Count > 0)
                                {
                                    if (dtU.Rows[0]["sysuserid"].ToString() != "")
                                    {
                                        url = dtU.Rows[0]["signaturepath"].ToString().Replace("..", "../..");
                                    }
                                }
                                else
                                {
                                    url = "../../App_Themes/images/white.bmp";
                                }
                                imgSignatrue.ImageUrl = url;

                                cell.Controls.Add(imgSignatrue);
                            }
                            else
                            {
                                imgSignatrue.ImageUrl = "../../App_Themes/images/white.bmp";
                                cell.Controls.Add(imgSignatrue);
                            }
                            row.Cells.Add(cell);

                            ht.Controls.Add(row);
                        }
                    }
                    #endregion 创建数据表格

                    row = CreateHTMLRow("20px", null, null);
                    row.Cells.Add(CreateHTMLCell("制表人：" + ((Model.Sys_User)(Session["SysUserInfo"])).USER_NAME.ToString(), "width:25%;font-size:11pt", "left", null, 3));
                    row.Cells.Add(CreateHTMLCell("打印时间：" + DateTime.Now.ToString("yyyy年MM月dd日"), "width:40%;font-size:11pt", "right", null, 3));
                    ht.Controls.Add(row);
                    row = CreateHTMLRow("20px", null, null);
                    ht.Controls.Add(row);

                    hlfdPrintTime.Value = DateTime.Now.ToString();
                    FillTableToTbody(ht, ref myTbody, allCount, pageCount, i);
                }
            }
            #endregion 方法
        }

        private string GetUserName(string UserId)
        {
            string sql = "select syu.user_name from sys_user syu where syu.user_id in (" + UserId + ")";
            string str = "&nbsp;";
            DataTable dtUser = new OracleHelper().GetDataTable(sql);
            if (dtUser != null && dtUser.Rows.Count > 0)
            {
                str = "";
                for (int i = 0; i < dtUser.Rows.Count; i++)
                {
                    str += dtUser.Rows[i][0].ToString() + "，";
                }
                str = str.Substring(0, str.Length - 1);
            }
            return str;
        }

        private HtmlGenericControl FillDataToHtmlTable(DataTable dt, HtmlGenericControl table, string dataCellClass)
        {
            DataRow dr = null;
            HtmlTableRow row = null;
            int rowCount = dt.Rows.Count;
            int cellCount = dt.Columns.Count - 5; //除去id列

            for (int j = 0; j < rowCount; j++)
            {
                dr = dt.Rows[j];
                row = CreateHTMLRow("20px", null, null);
                row.Cells.Add(CreateHTMLCell(dr["项目名称"].ToString() == "" ? "&nbsp;" : dr["项目名称"].ToString(), " border-top-width:0px;border-left-width:2px;border:1px solid; border-color:black; ", "center", dataCellClass, 3));
                row.Cells.Add(CreateHTMLCell(dr["截止上周资金计划"].ToString() == "" ? "&nbsp;" : dr["截止上周资金计划"].ToString(), "border-top-width:0px; border:1px solid;border-color:black;", "center", dataCellClass, 1));
                row.Cells.Add(CreateHTMLCell(dr["截止上周累计资金"].ToString() == "" ? "&nbsp;" : dr["截止上周累计资金"].ToString(), " border-top-width:0px; border:1px solid; border-color:black;", "center", dataCellClass, 1));
                row.Cells.Add(CreateHTMLCell(dr["本周资金计划"].ToString() == "" ? "&nbsp;" : dr["本周资金计划"].ToString(), "border-top-width:0px; border:1px solid; border-color:black;", "center", dataCellClass, 1));

                table.Controls.Add(row);
            }
            return table;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="rowCount"></param>
        /// <param name="table"></param>
        /// <param name="dataCellClass"></param>
        /// <returns></returns>
        private HtmlGenericControl FillEmptyDataToHtmlTable(int rowCount, HtmlGenericControl table, string dataCellClass)
        {
            HtmlTableRow row = null;
            for (int j = 0; j < rowCount; j++)
            {
                row = CreateHTMLRow("20px", null, null);
                row.Cells.Add(CreateHTMLCell("&nbsp;", " width:5% ; border-top-width:0px;border-left-width:2px;border:1px solid; border-color:black; ", "center", dataCellClass, 1));
                row.Cells.Add(CreateHTMLCell("&nbsp;", " width:15%; border-top-width:0px; border:1px solid; border-color:black;", "center", dataCellClass, 1));
                row.Cells.Add(CreateHTMLCell("&nbsp;", " width:15%; border-top-width:0px; border:1px solid; border-color:black; ", "center", dataCellClass, 1));
                row.Cells.Add(CreateHTMLCell("&nbsp;", " width:8%; border-top-width:0px; border:1px solid; border-color:black; ", "center", dataCellClass, 1));
                row.Cells.Add(CreateHTMLCell("&nbsp;", " width:15%;  border-top-width:0px;border-right-width:2px; border:1px solid; border-color:black;", "center", dataCellClass, 2));
                table.Controls.Add(row);
            }
            return table;
        }

        /// <summary>
        /// 添加数据到表头
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="currentPage"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        private HtmlGenericControl FillDataToHtmlHeadTable(DataTable dt)
        {
            string cellData = string.Empty;
            HtmlTableRow row = null;
            HtmlGenericControl ht = null;
            ht = CreateHTMLMainTable("100%", "border-bottom-width:0px", null);
            row = CreateHTMLRow("20px", null, "tr_pageHead");
            row.Cells.Add(CreateHTMLCell("周资金计划申请表（" + DateTime.Now.ToString("yyyy") + "年&nbsp;" + DateTime.Now.ToString("MM") + "月&nbsp;" + DateTime.Now.ToString("dd") + "日）", "font-size:20pt;width:90%;padding-Top:20px;", "center", null, 6));
            ht.Controls.Add(row);
            row = CreateHTMLRow("20px", null, "tr_pageHead");
            ht.Controls.Add(row);

            row = CreateHTMLRow(null, "border:0px solid; border-color:black;", null);
            row.Cells.Add(CreateHTMLCell("单位：" + dt.Rows[0]["公司名称"].ToString(), "font-size:12pt;border-color:black;", "left", null, 4));
            row.Cells.Add(CreateHTMLCell("金额单位: 元", "font-size:12pt;border-color:black;", "right", null, 2));
            ht.Controls.Add(row);

            row = CreateHTMLRow("20px", null, "tr_pageHead");
            ht.Controls.Add(row);

            row = CreateHTMLRow(null, "border:0px solid; border-color:black;", "tr_pageHead");

            row.Cells.Add(CreateHTMLCell("项目名称", "font-size:12pt; width:20%; border-left-width:2px;border:1px solid; border-color:black;font-weight:bold;", "center", "td_tableHead11", 3));
            row.Cells.Add(CreateHTMLCell("截止上周资金计划", "font-size:12pt; width:25%; border:1px solid; border-color:black;font-weight:bold;", "center", "td_tableHead11", 1));
            row.Cells.Add(CreateHTMLCell("截止上周累计资金情况", "font-size:12pt; width:20%; border:1px solid; border-color:black;font-weight:bold;", "center", "td_tableHead11", 1));
            row.Cells.Add(CreateHTMLCell("本周资金计划", "font-size:12pt; width:10%; border:1px solid; border-color:black;font-weight:bold;", "center", "td_tableHead11", 1));

            ht.Controls.Add(row);
            return ht;
        }

        private HtmlGenericControl FillDataToHtmlTotalTalbe(HtmlGenericControl table, int TotalAmount)
        {
            HtmlTableRow row = null;
            row = CreateHTMLRow(null, null, null);
            row.Cells.Add(CreateHTMLCell("&nbsp;", "border-left-width:2px;", "center", "td_tableData", 1));
            row.Cells.Add(CreateHTMLCell("合计", "", "left", "td_tableData", 2));
            row.Cells.Add(CreateHTMLCell("&nbsp;", "", "center", "td_tableData", 1));
            row.Cells.Add(CreateHTMLCell(TotalAmount.ToString(), "", "center", "td_tableData", 1));
            row.Cells.Add(CreateHTMLCell("&nbsp;", "border-right-width:2px;", "center", "td_tableData", 1));
            table.Controls.Add(row);
            return table;
        }

        private HtmlGenericControl FillEmptyDataToHtmlTotalTalbe(HtmlGenericControl table)
        {
            HtmlTableRow row = null;

            row = CreateHTMLRow(null, null, null);
            //  row.Cells.Add(CreateHTMLCell("备注", "border-left-width:2px; ", "center", "td_tableDataEnd11", 1));
            // row.Cells.Add(CreateHTMLCell(description, " ", "left", "td_tableDataEnd", 5, 1));

            row.Cells.Add(CreateHTMLCell("&nbsp;", "border-left-width:2px;", "center", "td_tableData", 1));
            row.Cells.Add(CreateHTMLCell("合计", "", "center", "td_tableData", 1));
            row.Cells.Add(CreateHTMLCell("&nbsp;", "", "center", "td_tableData", 1));
            row.Cells.Add(CreateHTMLCell("&nbsp;", "", "center", "td_tableData", 1));
            row.Cells.Add(CreateHTMLCell("&nbsp;", "", "center", "td_tableData", 1));
            row.Cells.Add(CreateHTMLCell("&nbsp;", "", "center", "td_tableData", 1));
            row.Cells.Add(CreateHTMLCell("&nbsp;", "", "center", "td_tableData", 1));
            row.Cells.Add(CreateHTMLCell("&nbsp;", "border-right-width:2px;", "center", "td_tableData", 1));

            table.Controls.Add(row);
            return table;
            //cell = CreateHTMLCell(null, "width:100%", null, null, 10);
            //cell.Controls.Add(table);

            //row = CreateHTMLRow(null, null, null);
            //row.Cells.Add(cell);
            //myTbody.Controls.Add(row);
        }

        /// 获取一页数据

        /// </summary>
        /// <param name="dataTable"></param>
        /// <param name="pageCount"></param>
        /// <param name="currentPage"></param>
        /// <returns></returns>
        private DataTable GetListDataOfOnePage(DataTable dataTable, int pageCount, int currentPage)
        {
            DataTable returnTable = dataTable.Clone();

            DataRow dr = null;
            int beginCount = (currentPage - 1) * pageCount;
            int endCount = dataTable.Rows.Count;
            for (int i = beginCount; i < endCount && i < currentPage * pageCount; i++)
            {
                dr = dataTable.Rows[i];
                returnTable.ImportRow(dr);
            }
            return returnTable;
        }

        private void FillTableToTbody(ref HtmlGenericControl tBody, int allCount, int pageCount, int currentPage)
        {
            HtmlTableRow row = null;
            ////row = CreateHTMLRow(null, null, null);
            //////tBody.Controls.Add(row);
            if (allCount > pageCount && allCount - pageCount * currentPage > 0)
            {
                row = CreateHTMLRow(null, "display:none;page-break-before:always", null);
                tBody.Controls.Add(row);
            }
        }

        private void FillTableToTbody(HtmlGenericControl table, ref HtmlGenericControl tBody, int allCount, int pageCount, int currentPage)
        {
            HtmlTableCell cell = null;
            HtmlTableRow row = null;
            cell = CreateHTMLCell(null, "width:100%", null, null, 12);
            cell.Controls.Add(table);

            row = CreateHTMLRow(null, null, null);
            row.Cells.Add(cell);
            tBody.Controls.Add(row);
            if (allCount > pageCount && allCount - pageCount * currentPage > 0)
            {
                row = CreateHTMLRow(null, "display:none;page-break-before:always", null);
                tBody.Controls.Add(row);
            }
        }

        #endregion

        #region 建表方法Create

        public HtmlTableCell CreateHTMLCell(string cellText, string cellStyle, string cellAlign, string cellClass, int cellCospan)
        {
            HtmlTableCell cell = new HtmlTableCell();
            return cell = CreateHTMLCell(cellText, cellStyle, cellAlign, cellClass, cellCospan, 1);
        }

        public HtmlTableCell CreateHTMLCell(string cellText, string cellStyle, string cellAlign, string cellClass, int cellCospan, int cellRowspan)
        {
            return CreateHTMLCell(cellText, cellStyle, cellAlign, null, cellClass, cellCospan, cellRowspan);
        }

        public HtmlTableCell CreateHTMLCell(string cellText, string cellStyle, string cellAlign, string cellValign, string cellClass, int cellCospan, int cellRowspan)
        {
            HtmlTableCell cell = new HtmlTableCell();
            if (cellText != null)
            {
                cell.InnerHtml = Server.HtmlDecode(cellText);
            }
            if (cellStyle != null)
            {
                cell.Attributes.Add("style", cellStyle);
            }
            if (cellCospan > 1)
            {
                cell.ColSpan = cellCospan;
            }
            if (cellAlign != null)
            {
                cell.Align = cellAlign;
            }
            if (cellValign != null)
            {
                cell.VAlign = cellValign;
            }
            if (cellClass != null)
            {
                cell.Attributes.Add("class", cellClass);
            }
            if (cellRowspan > 1)
            {
                cell.RowSpan = cellRowspan;
            }
            return cell;
        }

        public HtmlTableRow CreateHTMLRow(string rowHeight, string rowStyle, string rowClass)
        {
            HtmlTableRow row = new HtmlTableRow();
            if (rowHeight != null)
            {
                row.Height = rowHeight;
            }
            if (rowStyle != null)
            {
                row.Attributes.Add("style", rowStyle);
            }
            if (rowClass != null)
            {
                row.Attributes.Add("class", rowClass);
            }
            return row;
        }

        public HtmlTable CreateHTMLTable(string width, string tableClass)
        {
            return CreateHTMLTable(width, string.Empty, tableClass, "0", "0", "0");
        }

        public HtmlTable CreateHTMLTable(string width, string style, string tableClass)
        {
            return CreateHTMLTable(width, style, tableClass, "0", "0", "0");
        }

        public HtmlTable CreateHTMLTable(string width, string style, string tableClass, string tableBorderWidth, string cellspacingValue, string cellpaddingValue)
        {
            HtmlTable ht = new HtmlTable();
            ht.Attributes.Add("width", width);
            ht.Attributes.Add("style", style);
            ht.Attributes.Add("class", tableClass);
            ht.Attributes.Add("border", tableBorderWidth);
            ht.Attributes.Add("cellspacing", cellspacingValue);
            ht.Attributes.Add("cellpadding", cellpaddingValue);

            return ht;
        }

        public HtmlGenericControl CreateHTMLMainTable(string width, string tableClass)
        {
            return CreateHTMLMainTable(width, string.Empty, tableClass, "0", "0", "0");
        }

        public HtmlGenericControl CreateHTMLMainTable(string width, string style, string tableClass)
        {
            return CreateHTMLMainTable(width, style, tableClass, "0", "0", "0");
        }

        public HtmlGenericControl CreateHTMLMainTable(string width, string style, string tableClass, string tableBorderWidth, string cellspacingValue, string cellpaddingValue)
        {
            HtmlGenericControl ht = new HtmlGenericControl("table");
            ht.Attributes.Add("width", width);
            ht.Attributes.Add("style", style);
            ht.Attributes.Add("class", tableClass);
            ht.Attributes.Add("border", tableBorderWidth);
            ht.Attributes.Add("cellspacing", cellspacingValue);
            ht.Attributes.Add("cellpadding", cellpaddingValue);

            return ht;
        }

        #endregion
    }
}